import { LoginDto, ProposalDto } from './dtos'

const urlApi = 'http://194.87.145.144:5000/internship/v1/'

function ApiInstance(url: string, method: string, headers: Record<string, string>, body: BodyInit | null) {
    return Promise.resolve(
        fetch(url, {
            method,
            headers,
            body,
        })
    )
}

export function Authorization(userData: LoginDto) {
    return ApiInstance(
        urlApi + 'auth',
        'POST',
        {
            accept: '*/*',
            'Content-Type': 'application/json',
        },
        JSON.stringify(userData)
    ).then((response) => response.json())
}

export function GetUserInfo() {
    return ApiInstance(
        urlApi + 'user',
        'GET',
        {
            'Content-Type': 'application/json',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        null
    ).then((response) => response.json())
}

export function GetProposals() {
    return ApiInstance(
        urlApi + 'proposals',
        'GET',
        {
            accept: 'application/json',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        null
    ).then((response) => response.json())
}

export function GetProposalById(id: string) {
    return ApiInstance(
        urlApi + 'proposal/' + id,
        'GET',
        {
            accept: 'application/json',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        null
    ).then((response) => response.json())
}

export function GetDictionary(code: string) {
    return ApiInstance(
        urlApi + 'dictionary/' + code,
        'GET',
        {
            accept: 'application/json',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        null
    ).then((response) => response.json())
}

export function AddProposal(requestBody: ProposalDto) {
    return ApiInstance(
        urlApi + 'proposal',
        'POST',
        {
            accept: 'application/json',
            'Content-Type': 'application/json',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        JSON.stringify(requestBody)
    ).then((response) => response.json())
}

export function DeleteProposal(id: string) {
    return ApiInstance(
        urlApi + 'proposal/' + id,
        'DELETE',
        {
            accept: '*/*',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        null
    )
}

export function RewriteProposal(requestBody: ProposalDto) {
    return ApiInstance(
        urlApi + 'proposal',
        'PUT',
        {
            accept: 'application/json',
            'Content-Type': 'application/json',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        JSON.stringify(requestBody)
    ).then((response) => response.json())
}

export function SendProposal(id: string) {
    return ApiInstance(
        urlApi + 'proposal/' + id + '/send',
        'GET',
        {
            accept: '*/*',
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        null
    )
}
